package com.zjj.shiro.authroization;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.junit.Test;

public class AuthorizerTest {

	@Test
	public void testAuthorizer() {
		login("zjj", "123", "classpath:shiro-authorizer.ini");
		
		Subject subject = SecurityUtils.getSubject();
		System.out.println(subject.isPermitted("a"));
		System.out.println(subject.isPermitted("d"));
	}

	/**
	 * 用户登录
	 * 
	 * @param username
	 * @param password
	 * @param resourcePath
	 */
	private void login(String username, String password, String resourcePath) {
		Factory<SecurityManager> factory = new IniSecurityManagerFactory(resourcePath);
		SecurityManager securityManager = factory.getInstance();
		SecurityUtils.setSecurityManager(securityManager);

		UsernamePasswordToken token = new UsernamePasswordToken(username, password);
		Subject subject = SecurityUtils.getSubject();
		subject.login(token);
	}

}
